package com.view.resourceLoader
{
	/**
	 * 提供SWF功能加载
	 */	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	
	public class ResourceLoader
	{
		private static var resourceLoader:ResourceLoader;
		
		private var _loader:Loader;
		private var _path:String;
		private var _cls:Class;
		private var _callBack:Function = new Function();
		private var _resourceLoaderData:ResourceLoaderData;
		private var _resourceLoaderDataList:Array = [];//资源池
		
		/**
		 * resourceLoader 只是个标记
		 * 如果构造函数被执行了，resourceLoader就被赋值
		 */		
		public function ResourceLoader()
		{
			if(resourceLoader){
				throw Error('ResourceLoader实例不能被重复创建!');
			}else{
				resourceLoader = this;
			}
			trace('--->创建ResourceLoader实例!');
		}
		
		public static function getInstance():ResourceLoader{
			if(!resourceLoader) resourceLoader = new ResourceLoader();
			return resourceLoader;
		}
		
		/**
		 * 加载资源 
		 * @param path 路径
		 * @param swfName 名字
		 * @param callBack 加载完毕后执行的函数
		 */		
		public function load(path:String,swfName:String,callBack:Function):void{
			_path = path;
			_callBack = callBack;
			
			_resourceLoaderData = new ResourceLoaderData(path,swfName);
			
			_resourceLoaderDataList[swfName] = _resourceLoaderData;
			
			_resourceLoaderData.callBack = _callBack;
		}
		
		/**
		 * 获取所需Class 
		 * @param clsName
		 * @return 
		 */		
		public function getCls(clsName:String):Class{
			for (var key:String in _resourceLoaderDataList) 
			{
				if(_resourceLoaderDataList[key].getDefinition(clsName)){
					return _resourceLoaderDataList[key].getDefinition(clsName);
				}
			}
			return null;
		}
		
		/**
		 * 检查资源池中是否已存在目标资源 
		 * @param key
		 * @return true 存在 false 不存在
		 */		
		public function hasTargetSWF(swfName:String):Boolean{
			for(var key:String in _resourceLoaderDataList){
				if(key == swfName){
					trace('<---池中物:' + swfName + '.swf')
					return true;
				}
			}
			trace('--->非池中物:'+ swfName + '.swf');
			return false;
		}
		
		public function clearTargetSWF(swfName:String):void{
			if(_resourceLoaderDataList[swfName]){
				ResourceLoaderData(_resourceLoaderDataList[swfName]).dispose();
				_resourceLoaderDataList[swfName] = null;
				delete _resourceLoaderDataList[swfName];
			}
		}
		
	}
}